local sk__jiuzhu = fk.CreateSkill {

  name = "sk__jiuzhu",

  tags = {},

}



sk__jiuzhu:addEffect(fk.AfterCardsMove, {
  name = "sk__jiuzhu",
  anim_type = "offensive",
  can_trigger = function(self, event, target, player, data)
    if player:hasSkill(sk__jiuzhu.name) and not player:isNude() then
      local ids = {}
      for _, move in ipairs(data) do
        if move.toArea == Card.DiscardPile then
          for _, info in ipairs(move.moveInfo) do
            if Fk:getCardById(info.cardId).trueName == "jink" and table.contains(player.room.discard_pile, info.cardId) then
              table.insertIfNeed(ids, info.cardId)
            end
          end
        end
      end
      ids = player.room.logic:moveCardsHoldingAreaCheck(ids)
      if #ids > 0 then
        event:setCostData(self, ids)
        return true
      end
    end
  end,
  on_cost = function(self, event, target, player, data)
    local room = player.room
    if #event:getCostData(self) == 1 then
      local card = room:askForCard(player, 1, 1, true, sk__jiuzhu.name, true, "^jink",
        "#sk__jiuzhu-invoke:::" .. Fk:getCardById(event:getCostData(self)[1]):toLogString())
      if #card > 0 then
        event:setCostData(self, { card, event:getCostData(self) })
        return true
      end
    else
      local cards = table.filter(player:getCardIds("he"), function(id)
        return Fk:getCardById(id).trueName ~= "jink"
      end)
      local askForExchange = function(player, name1, name2, cards1, cards2, prompt, n, cancelable)
        return player.room:askForPoxi(player, "qml_exchange", {
          { name1, cards1 },
          { name2, cards2 },
        }, { prompt, n or 9999, cancelable == false }, (cancelable == nil) and true or cancelable)
      end

      local results = askForExchange(player, "pile_discard", "hand_card", event:getCostData(self), cards, "#sk__jiuzhu-exchange",999, true)
      if #results > 0 then
        event:setCostData(self, { table.filter(results, function(id)
          return table.contains(cards, id)
        end), table.filter(results, function(id)
          return table.contains(event:getCostData(self), id)
        end) })
        return true
      end
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    room:swapCardsWithPile(player, event:getCostData(self)[1], event:getCostData(self)[2], sk__jiuzhu.name, "discardPile", true, player)
    if not player.dead then
      local turn_event = room.logic:getCurrentEvent():findParent(GameEvent.Turn)
      if not turn_event then return end
      if player.phase == Player.NotActive and room.current and not room.current.dead and
          not player:isProhibited(room.current, Fk:cloneCard("slash")) and
          room:askForSkillInvoke(player, sk__jiuzhu.name, nil, "#sk__jiuzhu-slash::" .. room.current.id) then
        room:useVirtualCard("slash", nil, player, room.current, sk__jiuzhu.name, true)
      end
    end
  end,
})

sk__jiuzhu:addEffect(fk.TargetSpecified, {
  can_refresh = function(self, event, target, player, data)
    return target == player and table.contains(data.card.skillNames, "sk__jiuzhu")
  end,
  on_refresh = function(self, event, target, player, data)
    local room = player.room
    local to = data.to
    local use_event = room.logic:getCurrentEvent():findParent(GameEvent.UseCard, true)
    if use_event == nil then return end
    room:addPlayerMark(to, MarkEnum.MarkArmorNullified)
    use_event:addCleaner(function()
      room:removePlayerMark(to, MarkEnum.MarkArmorNullified)
    end)
  end,
})

return sk__jiuzhu
